{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "34c7261b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#宿舍人脸识别系统\n",
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "523e8afb",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = 'SMVJThrY1hoB4CDuwbI9kF1aBaZE7TNa'\n",
    "API_secret = 'Rbd67H3brlaPlhwaTgx8vwqkAop0ljBm'\n",
    "face_token01 = '03abf26fc554f43ee2ce78bd9f4bac5e'\n",
    "face_token02 = '1c3743bb59944e8bc36bc87362b434bc'\n",
    "face_token03 = 'd1a8b6a408dbd8c7a5f385307b98513a'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "72a21dde",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_create(API_key,API_secret):\n",
    "    url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "    payload ={\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_secret,\n",
    "    }\n",
    "    r = requests.post(url,payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "00d79040",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '0357c78f9fa114bc2e986abbc163d35e',\n",
       " 'time_used': 184,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1647948207,72608a18-2cbb-430a-8e49-4e35509995f9',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_create(API_key,API_secret,)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "67b261b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "faceset_token_one = face_create(API_key,API_secret)\n",
    "faceset_token_two = faceset_token_one['faceset_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "ea2ade46",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_addface(API_key,API_secret,faceset_token_two):\n",
    "    url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "    payload ={\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_secret,\n",
    "        'faceset_token':faceset_token_two,\n",
    "        'face_tokens':'d1a8b6a408dbd8c7a5f385307b98513a,1c3743bb59944e8bc36bc87362b434bc,03abf26fc554f43ee2ce78bd9f4bac5e'\n",
    "    }\n",
    "    r = requests.post(url,payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "a2c57ab7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '9a2368a3948b0da091d2a00c8a21188d',\n",
       " 'time_used': 946,\n",
       " 'face_count': 3,\n",
       " 'face_added': 3,\n",
       " 'request_id': '1647948631,66dceaec-83c2-4885-8907-9bf5860513f5',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_addface(API_key,API_secret,faceset_token_two)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "109b3cde",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_getfacesets(API_key,API_secret):\n",
    "    url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "    payload ={\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_secret,\n",
    "    }\n",
    "    r = requests.post(url,payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "e9d20725",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 73,\n",
       " 'facesets': [{'faceset_token': '4d75cbab7f70455249985acb149f0bec',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '0357c78f9fa114bc2e986abbc163d35e',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'cb68955ba6dc68bedd7d9a0d44ae5cd3',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '9a2368a3948b0da091d2a00c8a21188d',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1647948641,9439c635-a4dc-4824-879c-67b9765c6da6'}"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_getfacesets(API_key,API_secret)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "e2df9bad",
   "metadata": {},
   "outputs": [],
   "source": [
    "faceset_token_data = face_getfacesets(API_key,API_secret)\n",
    "faceset_token = faceset_token_data['facesets'][0]['faceset_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "38df2414",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入图片url\n"
     ]
    }
   ],
   "source": [
    "image_url = input(\"输入图片url\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "441f2891",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_detect(API_key,API_secret,image_url,beauty_score_min=0,beauty_score_max=100):\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload={\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_secret,\n",
    "        'image_url':image_url,\n",
    "        'beauty_score_min':beauty_score_min,\n",
    "        'beauty_score_max':beauty_score_max   \n",
    "    }\n",
    "    r = requests.post(url,payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "f66867ef",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'faces'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-48-aea72a52400f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mhuman_data\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mface_detect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mAPI_key\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mAPI_secret\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mimage_url\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mhuman_data_face_token\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mhuman_data\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'faces'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'face_token'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 'faces'"
     ]
    }
   ],
   "source": [
    "human_data = face_detect(API_key,API_secret,image_url)\n",
    "human_data_face_token = human_data['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "954d08c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'d1a8b6a408dbd8c7a5f385307b98513a'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "human_data_face_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a8f9c79e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_search(API_key,API_secret,face_token,facest_token,return_result_count=1):\n",
    "    url = \"https://api-cn.faceplusplus.com/facepp/v3/search\"\n",
    "    payload ={\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_secret,\n",
    "        'face_token':human_data_face_token,\n",
    "        'faceset_token':faceset_token,\n",
    "        'return_result_count':return_result_count\n",
    "    }\n",
    "    r = requests.post(url,payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74d44705",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_search(API_key,API_secret,human_data_face_token,face_tokens)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
